<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row><el-col><el-button type="primary">添加角色</el-button></el-col></el-row>

      <el-table :data="userList" border stripe>
        <!-- expand展开列   -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row :class="['bdbottom','juzhong',i1===0?'bdtop':'']" v-for="(item1,i1) in scope.row.children" :key="item1.id">
              <!-- 渲染一级权限 -->
              <el-col :span="5" ><el-tag closable @close="shanchusanjibiaoqian(scope.row,item1.id)">{{item1.authName}}</el-tag><i class="el-icon-caret-right"></i></el-col>
              <!-- 渲染二级和三级权限 closable可删除-->
              <el-col :span="19">
                <el-row :class="['juzhong',i2===0?'':'bdtop']" v-for="(item2,i2) in item1.children" :key="item2.id">
                  <el-col :span="10"><el-tag type="success" closable @close="shanchusanjibiaoqian(scope.row,item2.id)">{{item2.authName}}</el-tag><i class="el-icon-caret-right"></i></el-col>
                  <el-col :span="14"><el-tag type="warning" v-for="(item3) in item2.children" :key="item3.id" closable @close="shanchusanjibiaoqian(scope.row,item3.id)">{{item3.authName}}</el-tag ></el-col>
                </el-row>
              </el-col>
              </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting" @click="showSetRightsDialog(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
        
      </el-table>
    </el-card>

    <!-- 分配权限对话框  tree树形空间  show-checkbox可选框  node-key每个树节点用来作为唯一的标识 default-expand-all是否展开所有节点 
    default-checked-keys绑定勾选的数组-->
    <el-dialog
      title="分配权限"
      :visible.sync="SetRightsDialogVisible"
      width="3/0%">
      <el-tree :data="rightsData" :props="treeProps" show-checkbox node-key="id" default-expand-all :default-checked-keys="gouxaunshuzuid" ref="treeRef"></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="SetRightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRights">确 定</el-button>
      </span>
    </el-dialog>

    
  </div>
</template>

<script>
export default {
  data() {
    return {
      // rightsData分配权限拿到数据      treeProps树形空间的绑定对象   gouxaunshuzu勾选的数组
      userList:[],
      SetRightsDialogVisible:false,
      rightsData:[],

      treeProps:{
        label:'authName',
        children:'children'
      },
      gouxaunshuzuid:[],

    //当前即将分配权限的角色id
      roleId:''
         
    }
  },
  created() {
    this.getuserList()
  },
  methods: {
    async getuserList(){
      const{data:res}=await this.$http.get('roles')
      if(res.meta.status!==200){
        return this.$message.error('获取角色列表失败')
      }
      this.userList=res.data
      console.log(res.data)
  },

  //删除一，二，三级标签
  async shanchusanjibiaoqian(juese,id3){
    const shanchu=await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err)
        console.log(shanchu)
        if (shanchu !== 'confirm') {
        return this.$message.info('已经取消了删除')
      }

      const {data:res}=await this.$http.delete('roles/'+juese.id+'+/rights/'+id3+'')
      if(res.meta.status!==200){
        return this.$message.error("删除权限失败")
      }
      // this.getuserList()
      juese.children=res.data
      this.$message.success('删除权限成功！')
      // console.log('@@@',id3)  
  },

  //展示分配权限的对话框
  async showSetRightsDialog(role){
    this.roleId=role.id
    const {data:res}=await this.$http.get('rights/tree')
    if(res.meta.status!==200){
      return this.$$message.error('获取分配权限失败')
    }
    this.rightsData=res.data
    console.log(this.rightsData)

    //递归三级节点的id  清空数组，不然每次都会保存上一次的三级权限
    this.gouxaunshuzuid=[]
    this.getgouxaunshuzuid(role,this.gouxaunshuzuid)

    this.SetRightsDialogVisible=true

    
  },

  //通过递归的形式获取所有三级权限的id，并保存到gouxaunshuzuid
  getgouxaunshuzuid(node,arr){
    //node节点不包含children,是想要的三级节点
    if(!node.children){
      return arr.push(node.id)
    }
    node.children.forEach(item=>this.getgouxaunshuzuid(item,arr))
  },

  //分配权限对话框确认后，角色授权   
  //tree树里的一个方法getCheckedKeys返回目前被选中的节点的key所组成的数组
  //getHalfChecdKeys返回目前半选中的节点的key所组成的数组
  async allotRights(){
    
    const keys=[
      ...this.$refs.treeRef.getCheckedKeys(),
      ...this.$refs.treeRef.getHalfCheckedKeys()
    ]
    const idStr =keys.join(',')
    
    const {data:res}=await this.$http.post(`roles/${this.roleId}/rights`,{rids:idStr})
    if(res.meta.status!==200){
      this.$message.error('分配权限失败')
    }
    this.$message.success('分配权限成功')
    this.getuserList()
    this.SetRightsDialogVisible=false
  }

  
      
}
}
</script>

<style lang="less">
.el-tag{
  margin: 7px;
}
  .bdtop{
    border-top: 1px solid #eee;
  }
  .bdbottom{
    border-bottom: 1px solid #eee;
  }
  .juzhong{
    display: flex;
    align-items: center;
  }

</style>